package com.springup.trade.controller;

import com.springup.trade.entity.Result;
import com.springup.trade.entity.Transaction;
import com.springup.trade.exception.RequestException;
import com.springup.trade.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Brin
 * @date 20200623
 * @description security transact controller
 */
@RestController
public class TradeController {

    @PostMapping("/trade/inputBuy")
    public Result<Transaction> inputBuy(@RequestBody Transaction transaction){
        Result result = new Result();
        try{
            result.setData(transactionService.inputBuy(transaction));
        }catch (RequestException e){
            return Result.FAILURE(e.getCode(), e.getMessage());
        }
        return result;
    }

    @PostMapping("/trade/updateBuy")
    public Result<Transaction> updateBuy(@RequestBody Transaction transaction){
        Result result = new Result();
        try{
            result.setData(transactionService.updateBuy(transaction));
        }catch (RequestException e){
            return Result.FAILURE(e.getCode(), e.getMessage());
        }
        return result;
    }

    @PostMapping("/trade/cancelBuy")
    public Result<Transaction> cancelBuy(@RequestBody Transaction transaction){
        Result result = new Result();
        try{
            result.setData(transactionService.cancelBuy(transaction));
        }catch (RequestException e){
            return Result.FAILURE(e.getCode(), e.getMessage());
        }
        return result;
    }

    @PostMapping("/trade/outputSell")
    public Result<Transaction> outputSell(@RequestBody Transaction transaction){
        Result result = new Result();
        try{
            result.setData(transactionService.outputSell(transaction));
        }catch (RequestException e){
            return Result.FAILURE(e.getCode(), e.getMessage());
        }
        return result;
    }

    @PostMapping("/trade/updateSell")
    public Result<Transaction> updateSell(@RequestBody Transaction transaction){
        Result result = new Result();
        try{
            result.setData(transactionService.updateSell(transaction));
        }catch (RequestException e){
            return Result.FAILURE(e.getCode(), e.getMessage());
        }
        return result;
    }

    @PostMapping("/trade/cancelSell")
    public Result<Transaction> cancelSell(@RequestBody Transaction transaction){
        Result result = new Result();
        try{
            result.setData(transactionService.cancelSell(transaction));
        }catch (RequestException e){
            return Result.FAILURE(e.getCode(), e.getMessage());
        }
        return result;
    }


    @Autowired
    private TransactionService transactionService;
}
